<?php
session_start();

$id = $_GET['id'];
$op = $_GET['op'];

$cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : [];

for ($i = 0; $i < count($cart); $i++) {
   if ($cart[$i]['id'] == $id) {
      if ($op == 1) {
      	// 当前购买数量大于1,减少购买数量，否则删除购物车中该商品记录
      	if ($cart[$i]['num'] > 1) {
      	    $cart[$i]['num'] = $cart[$i]['num'] - 1;
      	} else {
      	    array_splice($cart, $i, 1);
      	}
      	
      } else {
      	$cart[$i]['num'] = $cart[$i]['num'] + 1;
      }
      break;
   }
}
$_SESSION['cart'] = $cart;
header('Location: ../cart.php');
